import {
    HOST
} from '../../../config';

function imgupload(context) {

    var bindPhotoDelete = function(e) {
        var index = typeof e === "number" ? e : e.currentTarget.dataset.index;
        var imgs = context.data.uploadData.images.filter((img, i) => {
            return i != index;
        });
        context.setData({
            "uploadData.images": imgs
        });
    };

    var bindChooseImage = function(e) {
        console.log( 'bindChooseImage' );
        console.log(e.currentTarget.dataset.count)
        wx.chooseImage({
            count: e.currentTarget.dataset.count || "9",
            sizeType: ['compressed'],
            success: (res) => {
                console.log(res)
                var paths = res.tempFilePaths;
                for (var i = 0, l = paths.length; i < l; i++) {
                    context.data.uploadData.images.push({
                        path: paths[i]
                    });
                }
                context.setData({
                    "uploadData.images": context.data.uploadData.images
                });
            }
        })
    };

    context.uploadImg = function(cd) {
        var imgs = JSON.parse(JSON.stringify(context.data.uploadData.images));
        wx.showLoading({
            title: '上传图片中',
        })
        var onAllUploaded = (imgs) => {
            wx.hideToast();
            if (imgs) {
                if(imgs.length!=0){
                    context.data.uploadData.images.map((item)=>{
                        if (item.src){
                            imgs.push(item)
                        }
                    })
                    context.setData({
                        "uploadData.images": imgs
                    })
                }
                cd()
                wx.hideLoading()
            } else {
                cd(false)
            }
        }
        var upload = (path, i) => {
            wx.uploadFile({
              url: `${HOST}/Member/appendPet`,
                filePath: path,
                name: 'file',
                success: (res) => {
                    var info = res.data.split(",")[1];
                    if (info.indexOf('http') != -1) {
                        imgs[i].src = info;
                    } else {
                        imgs.splice(i--, 1);
                    }
                    if (i < imgs.length - 1) {
                        upload(imgs[i + 1].path, i + 1);
                    } else {
                        onAllUploaded(imgs);
                    }
                },
                fail: (res) => {
                    imgs.splice(i--, 1);
                    if (i < imgs.length - 1) {
                        upload(imgs[i + 1].path, i + 1);
                    } else {
                        onAllUploaded(imgs);
                    }
                }
            });
        };
        if (imgs.length > 0) {
            upload(imgs[0].path, 0);
        } else {
            onAllUploaded(false)
        }
    }

    function init() {
        context[context.data.uploadData.onChooseImage] = bindChooseImage;
        context[context.data.uploadData.onPhotoDelete] = bindPhotoDelete;
    };

    return {
        init
    }
}

module.exports = imgupload;